﻿using MyCode.Project.Infrastructure;
using MyCode.Project.Infrastructure.Common;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web.Http.Filters;

namespace MyCode.Project.WebApi.App_Filter
{
    public class ResultHandleAttribute : System.Web.Http.Filters.ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            base.OnActionExecuted(actionExecutedContext);
            string requestDataStr = "";
            var stream = actionExecutedContext.Request.Content.ReadAsStreamAsync().Result;

            if (stream != null && stream.Length > 0)
            {
                stream.Position = 0; //当你读取完之后必须把stream的读取位置设为开始
                using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
                {
                    requestDataStr = reader.ReadToEnd().ToString();
                }
            }
            var sb = new StringBuilder(Environment.NewLine + "-----------------------------------------" + Environment.NewLine);
            sb.AppendLine(actionExecutedContext.Request.RequestUri.ToString());
            sb.AppendLine($"请求参数:{requestDataStr}");


            if (actionExecutedContext.Exception != null)
            { 
                sb.AppendLine($"Error:{actionExecutedContext.Exception}");
                LogHelper.Error(sb.ToString());
                throw actionExecutedContext.Exception;
            }
            var url = actionExecutedContext.Request.RequestUri.ToString();
            string token = $"{actionExecutedContext.Request.Headers.Authorization}";


            //if (!url.Contains("/api/Admin/"))
            //    LogHelper.Debug(new RequestModel { Url = url, Param = requestDataStr, Token = token });


            var actionContext = actionExecutedContext.ActionContext;

            //忽略结果过滤
            var ignore = actionContext.ActionDescriptor.GetCustomAttributes<IgnoreResultHandleAttribute>().Any() ||
                         actionContext.ControllerContext.ControllerDescriptor
                             .GetCustomAttributes<IgnoreResultHandleAttribute>().Any();
            if (!ignore)
            {
                var response = actionContext.Response;
                Result result = new Result();
                result.ResultCode = ResultCode.Success;
                if (response != null && response.Content != null)
                {
                    result.Data = response.Content.ReadAsAsync<object>().Result;
                }
                var json = JsonHelper.ToJson(result);
                LogHelper.Info(sb.ToString());
                LogHelper.Info("api返回: "+json);
                actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(System.Net.HttpStatusCode.OK, result, "application/json");
            }
            else
            {
                actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(System.Net.HttpStatusCode.OK);
            }
        }

    }
}